/***************************************************************************
 *   Copyright (C) 2008 by Goliash   *
 *   goliash@goliash.net   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
 #include "add_medium.h"

AddMediumDialog::AddMediumDialog(QWidget *parent) : QDialog(parent)
{
	setupUi(this);
	state_label->setText("--- loading ---");
	okButton->setDisabled(true);
	connect(refreshButton, SIGNAL(clicked()), this, SLOT(refresh()));

	prefs = Preferences::instance();
	setData(prefs);
}

void AddMediumDialog::mediumChanged(int state)
{
	if(state == -1) {
		state_label->setText(stateNotIn);
		name_in_db->setText("");
		okButton->setDisabled(true);
	}
	else {
		state_label->setText(stateIn);
		name_in_db->setText(medium->getOriginal_name());
		okButton->setDisabled(false);
	}	
}

void AddMediumDialog::setMedium(Medium *medium)
{
	this->medium = medium;
	path->setText(medium->getPath());
	device->setText(medium->getDevice());
}

void AddMediumDialog::refresh()
{
	state_label->setText("--- refreshing ---");
	okButton->setDisabled(true);
	//medium->scanISO9660();
	ScanThread *th = new ScanThread();
	th->setMedium(medium);
	th->start();
}

void AddMediumDialog::accept()
{
	prefs = Preferences::instance();
	getData(prefs);
	// postup pri zmacknuti Add:
	// overeni, pokud je mounted
	// pak skenovat folder
	// vypisovat, co se skenuje
	qDebug() << "adding of the medium accepted";
	medium->setName(this->name_in_db->text());
	this->close();
	
	//-------------------------------------
	// dodelat ... ihned. ...
	if(!medium->isMounted()) {
		medium->mount();
	}

	//-----------------------------------------

	// scanDialog()
	ScanDialog *scan = new ScanDialog(this->parentWidget(), medium);
	scan->show();
        //scan->scanMedium();
	// replacement by thread
	DiscoverThread *th = new DiscoverThread();
	th->setScanDialogWindow(scan);
        th->start();
}

void AddMediumDialog::reject()
{
	this->done(0);
	// to do
}

void AddMediumDialog::setData(Preferences *prefs)
{
	useFilterJPEG(prefs->filter_JPEG);
	useFilterTxt(prefs->filter_txt);
	useFilterMp3(prefs->filter_mp3);
	useFilterPdf(prefs->filter_pdf);
}

void AddMediumDialog::getData(Preferences *prefs)
{
	prefs->filter_JPEG = filterJPEG();
	prefs->filter_txt = filterTxt();
	prefs->filter_mp3 = filterMp3();
	prefs->filter_pdf = filterPdf();
}

void AddMediumDialog::useFilterJPEG(bool b)
{
	checkBox_JPEG->setChecked(b);
}

bool AddMediumDialog::filterJPEG()
{
	return checkBox_JPEG->isChecked();
}

void AddMediumDialog::useFilterTxt(bool b)
{
	checkBox_txt->setChecked(b);
}

bool AddMediumDialog::filterTxt()
{
	return checkBox_txt->isChecked();
}

void AddMediumDialog::useFilterMp3(bool b)
{
	checkBox_mp3->setChecked(b);
}

bool AddMediumDialog::filterMp3()
{
	return checkBox_mp3->isChecked();
}

void AddMediumDialog::useFilterPdf(bool b)
{
	checkBox_pdf->setChecked(b);
}

bool AddMediumDialog::filterPdf()
{
	return checkBox_pdf->isChecked();
}
